package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.sys.OrgModel;
import cn.com.yx.ms.service.ISysService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "/")
@Api(value = "组织机构-部门", description = "组织结构-部门相关操作接口")
public class DeptController {

    private static final Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Resource
    private MessageSource messageSource;

    @Resource
    private ISysService iSysService;


    @PostMapping("dept")
    @ApiOperation(value = "创建部门信息", notes = "创建部门信息")
    @ApiImplicitParam(paramType = "body", name = "orgModel", dataType = "Map", required = true, value = "部门实体详细信息")
    public ApiResult create(@RequestBody @Validated OrgModel orgModel, BindingResult bindingResult) {
        return iSysService.saveOrg(orgModel);
    }

    @DeleteMapping("depts/{id}")
    @ApiOperation(value = "删除部门信息", notes = "删除部门信息")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "部门id")
    public ApiResult delete(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.deleteOrg(id));
    }

    @PutMapping("dept")
    @ApiOperation(value = "更新部门信息", notes = "更新部门信息")
    @ApiImplicitParam(paramType = "body", name = "orgModel", dataType = "Map", required = true, value = "部门信息")
    public ApiResult updateModel(@RequestBody @Validated OrgModel orgModel, BindingResult bindingResult) {
        return iSysService.saveOrg(orgModel);
    }

    @PatchMapping("dept")
    @ApiOperation(value = "部门信息部分更新", notes = "部门信息部分更新")
    @ApiImplicitParam(paramType = "body", name = "orgModel", dataType = "Map", required = true, value = "部门信息")
    public ApiResult updateProperty(@RequestBody @Validated OrgModel orgModel, BindingResult bindingResult) {
        //return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "更新成功", iTestService.update(testModel));
        return null;
    }


    @ResponseBody
    @RequestMapping(value = "depts", method = RequestMethod.GET)
    @ApiOperation("获取所有部门信息")
    public ApiResult find(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        query.getConditions().put("orgname", request.getParameter("orgname"));
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                "",
                iSysService.findOrg(query));

    }

    @ResponseBody
    @RequestMapping(value = "depts/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的部门信息")
    public ApiResult findPage(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        //System.out.println(BaseConstant.getAesConfig().getSecretKey());

        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("title", request.getParameter("title"));
        query.getConditions().put("count", request.getParameter("count"));
        query.getConditions().put("starttime", request.getParameter("starttime"));
        query.getConditions().put("endtime", request.getParameter("endtime"));

        //return new ApiResult<>(AppConstant.MSG_SUCCESS, iTestService.find(query));
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "depts/{id}", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "测试ID")
    @ApiOperation("测试接口")
    public ApiResult findById(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.findOneOrg(id));
    }

}